package com.onstar.cn.weight.more;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.weight.R;

/**
 * Created by 7828wc on 16/6/27.
 */
public class CustomLineLayout extends LinearLayout {
    private View view;
    private TextView tv_text;
    private TextView tv_right_text;
    private ImageView image_icon;
    private ImageView image_right_icon, red_point;
    private ProgressBar request_loading;
    private ToggleButton switchBtn;

    public CustomLineLayout(Context context) {
        super(context);
        initView(null);
    }

    public CustomLineLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public CustomLineLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.weight_line_text, this);
        tv_text = (TextView) view.findViewById(R.id.tv_text);
        request_loading = (ProgressBar) view.findViewById(R.id.request_loading);
        tv_right_text = (TextView) view.findViewById(R.id.tv_right_text);
        image_icon = (ImageView) view.findViewById(R.id.image_icon);
        red_point = (ImageView) view.findViewById(R.id.red_point);
        image_right_icon = (ImageView) view.findViewById(R.id.image_right_icon);
        switchBtn = (ToggleButton) view.findViewById(R.id.switchBtn);

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.input);
            String title = typedArray.getString(R.styleable.input_topTitle);
            tv_text.setText(title);

            String rightText = typedArray.getString(R.styleable.input_rightText);
            if (!CommonUtil.isNull(rightText)) {
                tv_right_text.setText(rightText);
                tv_right_text.setVisibility(View.VISIBLE);
            } else {
                tv_right_text.setVisibility(View.GONE);
            }

            int imgSrc = typedArray.getResourceId(R.styleable.input_src, -1);
            if (imgSrc != -1) {
                image_icon.setImageResource(imgSrc);
                image_icon.setVisibility(View.VISIBLE);
            } else {
                image_icon.setVisibility(View.INVISIBLE);
            }

            boolean arrowShow = typedArray.getBoolean(R.styleable.input_showRightArrow, true);
            if (arrowShow) {
                image_right_icon.setVisibility(View.VISIBLE);
            } else {
                image_right_icon.setVisibility(View.INVISIBLE);
            }

            boolean showSwitch = typedArray.getBoolean(R.styleable.input_showSwitch, false);
            if (showSwitch) {
                switchBtn.setVisibility(View.VISIBLE);
            } else {
                switchBtn.setVisibility(View.INVISIBLE);
            }

            typedArray.recycle();
        }
    }

    public void showLoading(boolean isShow) {
        tv_right_text.setVisibility(isShow ? View.GONE : View.VISIBLE);
        request_loading.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public boolean isShowLoading() {
        return request_loading.getVisibility() == View.VISIBLE;
    }

    public boolean isShowRightArrow() {
        return image_right_icon.getVisibility() == View.VISIBLE;
    }

    public void showRightArrow(boolean isShow) {
        if (isShow) {
            image_right_icon.setVisibility(View.VISIBLE);
        } else {
            image_right_icon.setVisibility(View.INVISIBLE);
        }
    }

    public void setRightArrow(int resId) {
        if (resId != -1) {
            image_right_icon.setImageResource(resId);
        }
        showRightArrow(resId != -1);
    }

    public void showMark(boolean isShow) {
        red_point.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public void setText(String text) {
        tv_text.setText(text);
    }

    public void setRightText(String rightText) {
        if (!CommonUtil.isNull(rightText)) {
            tv_right_text.setText(rightText);
            tv_right_text.setVisibility(View.VISIBLE);
        } else {
            tv_right_text.setVisibility(View.GONE);
        }
    }

    public String getRightText() {
        return tv_right_text.getText().toString();
    }

    public String getText() {
        return tv_text.getText().toString();
    }

    public void setTextColor(int resColor) {
        tv_text.setTextColor(getContext().getResources().getColor(resColor));
    }

    public void setTextSize(int textSize) {
        tv_text.setTextSize(textSize);
    }

    public void setText(int resId) {
        tv_text.setText(getContext().getString(resId));
    }

    public void setImageIcon(int resId) {
        if (resId != -1) {
            image_icon.setImageResource(resId);
            image_icon.setVisibility(View.VISIBLE);
        } else {
            image_icon.setVisibility(View.INVISIBLE);
        }
    }

    public void setSwitchCallback(CompoundButton.OnCheckedChangeListener changeListener) {
        switchBtn.setOnCheckedChangeListener(changeListener);
    }

//    public void setChecked(boolean checked) {
//        switchBtn.setChecked(checked);
//    }

    public void setSelected(boolean selected) {
        switchBtn.setSelected(selected);
    }

    public void setSwitchEnable(boolean enable) {
        switchBtn.setEnabled(enable);
    }

    public void setSwitchVisable(boolean enable) {
        switchBtn.setVisibility(enable?VISIBLE:GONE);
    }

}
